import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'home',
    component: () => import('../views/HomeView.vue'),
    meta: { title: '首页' }
  },
  {
    path: '/publish',
    name: 'publish',
    component: () => import('../views/PublishView.vue'),
    meta: { title: '发布商品', requiresAuth: true }
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/AboutView.vue'),
    meta: { title: '关于我们' }
  },
  {
    path: '/help',
    name: 'help',
    component: () => import('../views/HelpView.vue'),
    meta: { title: '帮助中心' }
  },
  {
    path: '/privacy',
    name: 'privacy',
    component: () => import('../views/PrivacyView.vue'),
    meta: { title: '隐私政策' }
  },
  {
    path: '/contact',
    name: 'contact',
    component: () => import('../views/ContactView.vue'),
    meta: { title: '联系我们' }
  },
  {
    path: '/faq',
    name: 'faq',
    component: () => import('../views/FAQView.vue'),
    meta: { title: '常见问题' }
  },
  {
    path: '/terms',
    name: 'terms',
    component: () => import('../views/TermsView.vue'),
    meta: { title: '服务条款' }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/user/LoginView.vue'),
    meta: { title: '登录', guest: true }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/user/RegisterView.vue'),
    meta: { title: '注册', guest: true }
  },
  {
    path: '/user/profile',
    name: 'userProfile',
    component: () => import('../views/user/ProfileView.vue'),
    meta: { title: '个人信息', requiresAuth: true }
  },
  {
    path: '/products',
    name: 'products',
    component: () => import('../views/product/ProductListView.vue'),
    meta: { title: '商品列表' }
  },
  {
    path: '/product/:id',
    name: 'productDetail',
    component: () => import('../views/product/ProductDetailView.vue'),
    meta: { title: '商品详情' }
  },
  // 保留原有的完整路径作为备用
  {
    path: '/product/publish',
    name: 'publishProduct',
    redirect: '/publish'
  },
  {
    path: '/product/edit/:id',
    name: 'editProduct',
    component: () => import('../views/product/EditProductView.vue'),
    meta: { title: '编辑商品', requiresAuth: true }
  },
  {
    path: '/cart',
    name: 'cart',
    component: () => import('../views/trade/CartView.vue'),
    meta: { title: '购物车', requiresAuth: true }
  },
  {
    path: '/orders',
    name: 'orders',
    component: () => import('../views/order/OrderListView.vue'),
    meta: { title: '我的订单', requiresAuth: true }
  },
  {
    path: '/order/:id',
    name: 'orderDetail',
    component: () => import('../views/order/OrderDetailView.vue'),
    meta: { title: '订单详情', requiresAuth: true }
  },
  {
    path: '/order/confirm',
    name: 'confirmOrder',
    component: () => import('../views/order/ConfirmOrderView.vue'),
    meta: { title: '确认订单', requiresAuth: true }
  },
  {
    path: '/admin',
    name: 'admin',
    component: () => import('../views/admin/DataStatisticsView.vue'),
    meta: { title: '后台管理', requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/admin/products',
    name: 'adminProducts',
    component: () => import('../views/admin/ProductAuditView.vue'),
    meta: { title: '商品审核', requiresAuth: true, requiresAdmin: true }
  },
  {
    path: '/admin/users',
    name: 'adminUsers',
    component: () => import('../views/admin/UserManagementView.vue'),
    meta: { title: '用户管理', requiresAuth: true, requiresAdmin: true }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    // 平滑滚动到顶部
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0, behavior: 'smooth' }
    }
  }
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - 校园二手交易平台`
  } else {
    document.title = '校园二手交易平台'
  }
  
  // 模拟检查用户登录状态
  const isLoggedIn = localStorage.getItem('token')
  
  // 路由守卫逻辑
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 需要登录的页面
    if (!isLoggedIn) {
      // 未登录则跳转到登录页
      next({ 
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      // 检查是否需要管理员权限
      if (to.matched.some(record => record.meta.requiresAdmin)) {
        // 模拟检查管理员权限
        const isAdmin = localStorage.getItem('isAdmin') === 'true'
        if (!isAdmin) {
          next({ path: '/' })
        } else {
          next()
        }
      } else {
        next()
      }
    }
  } else if (to.matched.some(record => record.meta.guest)) {
    // 游客页面（登录/注册）
    if (isLoggedIn) {
      // 已登录则跳转到首页
      next({ path: '/' })
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router